import datetime
import json
import os

import portalocker
from flask import Flask, request

from Logger import logger
from Utilities import validate_title

logger.info('AppCI starts')
app = Flask(__name__)


@app.route('/imr-ai-service/face_features/check/<file_id>', methods=['POST'])
def check(file_id):
    if request.method == "POST":

        f_name = os.path.join('Tasks', validate_title('check_' + str(datetime.datetime.now()) + '.json'))
        with open(f_name, 'w') as task_file:
            portalocker.lock(task_file, portalocker.LOCK_EX)
            json.dump(
                {
                    'task_id': {
                        'main': 'face',
                        'sub': 'check'
                    },
                    'param_keys': ['file_id'],
                    'param_dict': {
                        'file_id': file_id
                    }
                },
                task_file
            )
        return json.dumps(
            {
                'ret': 0,
                'msg': 'fake msg',
                'data': 'fake result'
            },
            ensure_ascii=False
        )
